<template>
  <el-form ref="accountList" status-icon :model="accountList" :rules="rules" label-width="100px">
    <el-form-item label="原密码" prop="oldPassword">
    <el-input v-model="accountList.oldPassword"></el-input>
    </el-form-item>

    <el-form-item label="新密码" prop="newPassword" autocomplete="off">
    <el-input v-model="accountList.newPassword"></el-input>
    </el-form-item>

    <el-form-item label="确认新密码" prop="checkPassword" autocomplete="off">
    <el-input v-model="accountList.checkPassword"></el-input>
    </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submit">确定</el-button>
    <el-button @click="reset">重置</el-button>
  </el-form-item>
</el-form>
</template>

<script>
import { editpwd, checkoldpwd } from "@/api/Account/account.api";
import { Message } from "element-ui";
export default {
  data() {
    let oldPassword = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入旧密码"));
      } else {
        if (value == 1) {
          callback(new Error("请输入旧密码"));
        }
        callback();
      }
    };
    let newPassword = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入新密码"));
      } else {
        this.$refs.accountList.validateField("checkPassword");
      }
      callback();
    };
    let checkPassword = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.accountList.newPassword) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      accountList: {
        oldPassword: "",
        newPassword: "",
        checkPassword: ""
      },
      rules: {
        oldPassword: [{ validator: oldPassword, trigger: "blur" }],
        newPassword: [{ validator: newPassword, trigger: "blur" }],
        checkPassword: [{ validator: checkPassword, trigger: "blur" }]
      },
      newPassword: {}
    };
  },
  methods: {
    submit() {
      this.$refs.accountList.validate(valid => {
        if (valid) {
          editpwd(this.accountList)
            .then(res => {
              console.log(res);
              Message({
                type: "success",
                message: "修改成功!"
              });
            })
            .catch(() => {
              Message({
                type: "info",
                message: "修改失败!"
              });
            });
        }
      });
    },
    reset() {
      this.$refs.accountList.resetFields();
    }
  }
};
</script>

<style scoped>
.el-input--small {
  width: 20%;
}
</style>